﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.COM;
using MINERP.DAL;
using MINERP.DAL.InnerFactory;
using MINERP.MODEL.FoundationModel;
using System.Data;


namespace MINERP.BLL.InnerFactory
{
    public class InnerFactoryBLL
    {
        private InnerFactoryDAL _dal = new InnerFactoryDAL();


        public List<ProduceView> NewGetProduceViewList(int inFactoryId)
        {
            List<ProduceView> ProduceViewLists = new List<ProduceView>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                int Status = (int)OutProcessRecordStatus.Fetched;
                int Status2 = (int)OutProcessRecordStatus2.Complete;
                int OrderStatus = (int)OrderInfoStatus.Doing;

                List<OutProcessRecord> outProcessRecords =  db.OutProcessRecord.Where(p => p.orderInfo.status == OrderStatus && p.OutProcessShop.OutShopId == inFactoryId && p.Status != Status && p.Status2 != (int)Status2).ToList();
                               
                foreach (OutProcessRecord list in outProcessRecords)
                {
                    ProduceView newView = new ProduceView();
                    if (!list.orderInfoReference.IsLoaded)
                    {
                        list.orderInfoReference.Load();
                    }
                    orderInfo product = list.orderInfo;
                    product.Tb_ProductReference.Load();
                    Tb_Product productType = product.Tb_Product;
                    newView.Product = new OrderInfo_Info() { ProductId = product.OrderProductId, CompleteTime = product.CompleteTime, CtmName = product.CtmName, Height = product.Height, Width = product.Width, OrderProductCode = product.OrderProductCode, Remark = product.Remark, OrderProductName = product.OrderProductName, PageNum = product.PageNum, OrderPath = product.OrderPath };
                    newView.ProductType = new Tb_Product_Info() { ProuctId = productType.ProuctId, ProductName = productType.ProductName };

                    if (!product.Tb_OrderBagReference.IsLoaded)
                    {
                        product.Tb_OrderBagReference.Load();
                    }
                    Tb_OrderBag orderBag = product.Tb_OrderBag;
                    orderBag.ShopReference.Load();
                    newView.Order = new Tb_OrderBag_Info() { BagCode = orderBag.BagCode, BagName = orderBag.BagName, OrderBagId = orderBag.OrderBagId, ShopID = orderBag.Shop.ShopId, ShopName = orderBag.Shop.shopName, FetchTime = orderBag.FetchTime };
                    newView.ProduceInfo = new OutProcessRecord_Info() { CreateTime = list.CreateTime, EndProductTime = list.EndProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.EndProductTime, StartProductTime = list.StartProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.StartProductTime, Status = list.Status, Status2 = list.Status2, OutRemark = list.OutRemark, Remark = list.Remark };
      
                    list.OutProcessShopReference.Load();
                    OutProcessShop shop = list.OutProcessShop;
                    newView.Factory = new OutProcessShop_Info() { OutShopId = shop.OutShopId, OutShopName = shop.OutShopName };
                    //switch (newView.ProduceInfo.Status)
                    //{
                    //    case (int)OutProcessRecordStatus.Outing:
                    //        {
                    //            switch (newView.ProduceInfo.Status2)
                    //            {
                    //                case (int)OutProcessRecordStatus2.Doing:
                    //                    newView.Status = "正在生产";
                    //                    break;
                    //                case (int)OutProcessRecordStatus2.Error:
                    //                    newView.Status = "暂停生产";
                    //                    break;
                    //                case (int)OutProcessRecordStatus2.Complete:
                    //                    newView.Status = "生产完成";
                    //                    break;
                    //                default:
                    //                    newView.Status = "等待接收";
                    //                    break;
                    //            }
                    //        }
                    //        break;
                    //    case (int)OutProcessRecordStatus.InGrid:
                    //        {
                    //            newView.Status = "未打印(在格子中)";
                    //        }
                    //        break;
                    //    case (int)OutProcessRecordStatus.OutWaitFenDui:
                    //        {
                    //            newView.Status = "未打印(等待分堆)";
                    //        }

                    //        break;
                    //}
                    productType.Tb_OutShop_ProductCategory.Load();
                    Tb_OutShop_ProductCategory pc = productType.Tb_OutShop_ProductCategory.Where(p => p.OutShopId == shop.OutShopId).FirstOrDefault();
                    if (pc != null)
                    {
                        newView.TimeSpan = pc.NeedTime;
                    }
                    else
                    {
                        newView.TimeSpan = 0;
                    }          
                    ProduceViewLists.Add(newView);
                }
            }
            return ProduceViewLists;
        }
        public ProduceView NewGetProduceViewListByOutProcessID(int outProcessProductid)
        {
            ProduceView rtnView = new ProduceView();
            //List<ProduceView> ProduceViewLists = new List<ProduceView>();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                int Status = (int)OutProcessRecordStatus.Fetched;
                int Status2 = (int)OutProcessRecordStatus2.Complete;
                int OrderStatus = (int)OrderInfoStatus.Doing;

                List<OutProcessRecord> outProcessRecords = db.OutProcessRecord.Where(p => p.OutProcessRecordId == outProcessProductid).ToList();

                foreach (OutProcessRecord list in outProcessRecords)
                {
                    ProduceView newView = new ProduceView();
                    if (!list.orderInfoReference.IsLoaded)
                    {
                        list.orderInfoReference.Load();
                    }
                    orderInfo product = list.orderInfo;
                    product.Tb_ProductReference.Load();
                    Tb_Product productType = product.Tb_Product;
                    newView.Product = new OrderInfo_Info() { ProductId = product.OrderProductId, CompleteTime = product.CompleteTime, CtmName = product.CtmName, Height = product.Height, Width = product.Width, OrderProductCode = product.OrderProductCode, Remark = product.Remark, OrderProductName = product.OrderProductName, PageNum = product.PageNum, OrderPath = product.OrderPath };
                    newView.ProductType = new Tb_Product_Info() { ProuctId = productType.ProuctId, ProductName = productType.ProductName };

                    if (!product.Tb_OrderBagReference.IsLoaded)
                    {
                        product.Tb_OrderBagReference.Load();
                    }
                    Tb_OrderBag orderBag = product.Tb_OrderBag;
                    orderBag.ShopReference.Load();
                    newView.Order = new Tb_OrderBag_Info() { BagCode = orderBag.BagCode, BagName = orderBag.BagName, OrderBagId = orderBag.OrderBagId, ShopID = orderBag.Shop.ShopId, ShopName = orderBag.Shop.shopName, FetchTime = orderBag.FetchTime };
                    newView.ProduceInfo = new OutProcessRecord_Info() { CreateTime = list.CreateTime, EndProductTime = list.EndProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.EndProductTime, StartProductTime = list.StartProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.StartProductTime, Status = list.Status, Status2 = list.Status2, OutRemark = list.OutRemark, Remark = list.Remark };

                    list.OutProcessShopReference.Load();
                    OutProcessShop shop = list.OutProcessShop;
                    newView.Factory = new OutProcessShop_Info() { OutShopId = shop.OutShopId, OutShopName = shop.OutShopName };
                    
                    productType.Tb_OutShop_ProductCategory.Load();
                    Tb_OutShop_ProductCategory pc = productType.Tb_OutShop_ProductCategory.Where(p => p.OutShopId == shop.OutShopId).FirstOrDefault();
                    if (pc != null)
                    {
                        newView.TimeSpan = pc.NeedTime;
                    }
                    else
                    {
                        newView.TimeSpan = 0;
                    }
                    //ProduceViewLists.Add(newView);
                    rtnView = newView;
                }
            }
            return rtnView;
        }
        /// <summary>
        /// 获取生产列表
        /// </summary>
        /// <param name="inFactoryId"></param>
        /// <returns></returns>
        public List<ProduceView> GetProduceViewList(int inFactoryId)
        {
            List<ProduceView> ProduceViewLists = new List<ProduceView>();
            List<OutProcessRecord> outProcessRecords = _dal.GetOutRocord(inFactoryId);
            foreach (OutProcessRecord list in outProcessRecords)
            {
                ProduceView newView = new ProduceView();
                if (!list.orderInfoReference.IsLoaded)
                {
                    list.orderInfoReference.Load();
                }
                orderInfo product = list.orderInfo;
                product.Tb_ProductReference.Load();
                Tb_Product productType = product.Tb_Product;
                newView.Product = new OrderInfo_Info() { ProductId = product.OrderProductId, CompleteTime = product.CompleteTime, CtmName = product.CtmName, Height = product.Height, Width = product.Width, OrderProductCode = product.OrderProductCode, Remark = product.Remark, OrderProductName = product.OrderProductName, PageNum = product.PageNum,OrderPath=product.OrderPath };
                newView.ProductType = new Tb_Product_Info() { ProuctId = productType.ProuctId, ProductName = productType.ProductName };

                if (!product.Tb_OrderBagReference.IsLoaded)
                {
                    product.Tb_OrderBagReference.Load();
                }
                Tb_OrderBag orderBag = product.Tb_OrderBag;
                orderBag.ShopReference.Load();
                newView.Order = new Tb_OrderBag_Info() { BagCode = orderBag.BagCode, BagName = orderBag.BagName, OrderBagId = orderBag.OrderBagId, ShopID = orderBag.Shop.ShopId, ShopName = orderBag.Shop.shopName,FetchTime = orderBag.FetchTime};

                newView.ProduceInfo = new OutProcessRecord_Info() { CreateTime = list.CreateTime, EndProductTime = list.EndProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.EndProductTime, StartProductTime = list.StartProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)list.StartProductTime, Status = list.Status, Status2 = list.Status2, OutRemark = list.OutRemark, Remark = list.Remark };
                //if (newView.ProduceInfo.Status == (int)OutProcessRecordStatus.InGrid || newView.ProduceInfo.Status == (int)OutProcessRecordStatus.OutWaitFenDui || newView.ProduceInfo.Status == (int)OutProcessRecordStatus.Outing)
                //{
                //    if(newView.ProduceInfo.Status2 == (int)OutProcessRecordStatus2.InGrid)

                //}
                list.OutProcessShopReference.Load();
                OutProcessShop shop = list.OutProcessShop;
                newView.Factory = new OutProcessShop_Info() { OutShopId = shop.OutShopId, OutShopName = shop.OutShopName };
                //switch (newView.ProduceInfo.Status)
                //{
                //    case (int)OutProcessRecordStatus.Outing:
                //        {
                //            switch (newView.ProduceInfo.Status2)
                //            {
                //                case (int)OutProcessRecordStatus2.Doing:
                //                    newView.Status = "正在生产";
                //                    break;
                //                case (int)OutProcessRecordStatus2.Error:
                //                    newView.Status = "暂停生产";
                //                    break;
                //                case (int)OutProcessRecordStatus2.Complete:
                //                    newView.Status = "生产完成";
                //                    break;
                //                default:
                //                    newView.Status = "等待接收";
                //                    break;
                //            }
                //        }
                //        break;
                //    case (int)OutProcessRecordStatus.InGrid:
                //        {
                //            newView.Status = "未打印(在格子中)";
                //        }
                //        break;
                //    case (int)OutProcessRecordStatus.OutWaitFenDui:
                //        {
                //            newView.Status = "未打印(等待分堆)";
                //        }
                  
                //        break;
                //}
                productType.Tb_OutShop_ProductCategory.Load();
                Tb_OutShop_ProductCategory pc = productType.Tb_OutShop_ProductCategory.Where(p => p.OutShopId == shop.OutShopId).FirstOrDefault();
                if (pc != null)
                {
                    newView.TimeSpan = pc.NeedTime;
                }
                else
                {

                    newView.TimeSpan = 0;
                }
                //newView.PreCompleteDateTime(
                ProduceViewLists.Add(newView);
            }
            return ProduceViewLists;
        }

        //public int StartProduce(string productBarCode, int inUser)
        //{
        //    OutProcessRecord outProcessRecord = _dal.GetOutRecordByBarcode(productBarCode);
        //    if (outProcessRecord == null)
        //    {
        //        return -1;
        //    }
        //    if (outProcessRecord.Status != (int)OutProcessRecordStatus.Outing)
        //    {
        //        return 0;
        //    }
        //    if (outProcessRecord.Status == (int)OutProcessRecordStatus.Outing)
        //    {
        //        if (outProcessRecord.Status2 == (int)OutProcessRecordStatus2.Wait)
        //        {
        //            //正常生产
        //            outProcessRecord.StartProductTime = DateTime.Now;
        //            outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Doing;
        //            outProcessRecord.OperateUser = inUser;
        //            _dal.SaveData();
        //        }
        //        else if (outProcessRecord.Status2 == (int)OutProcessRecordStatus2.Doing)
        //        {
        //            //结束生产

        //            outProcessRecord.EndProductTime = DateTime.Now;
        //            outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Complete;
        //            outProcessRecord.OperateUser = inUser;
        //            _dal.SaveData();
        //        }
        //        else if (outProcessRecord.Status2 == (int)OutProcessRecordStatus2.Error)
        //        {
        //            //错误的恢复生产
        //            outProcessRecord.StartProductTime = DateTime.Now;
        //            outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Doing;
        //            outProcessRecord.OperateUser = inUser;
        //            _dal.SaveData();
        //        }
        //        else
        //        {
        //            ///完成的不处理
        //        }
        //    }
        //}
        /// <summary>
        /// 开始生产
        /// </summary>
        /// <param name="inUser"></param>
        /// <param name="inStrErr"></param>
        /// <param name="inoutRecordId"></param>
        public void StartProduce(int inUser, int inoutRecordId)
        {
            //OutProcessRecord outProcessRecord = _dal.GetOutRecordById(inoutRecordId);
            //outProcessRecord.StartProductTime = DateTime.Now;
            //outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Doing;
            //outProcessRecord.OperateUser = inUser;
            //_dal.SaveData();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessRecord outProcessRecord = db.OutProcessRecord.FirstOrDefault(p=>p.OutProcessRecordId==inoutRecordId);
                outProcessRecord.StartProductTime = DateTime.Now;
                outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Doing;
                outProcessRecord.OperateUser = inUser;
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 结束生产
        /// </summary>
        /// <param name="inUser"></param>
        /// <param name="inStrErr"></param>
        /// <param name="inoutRecordId"></param>
        public void EndProduce(int inUser, int inoutRecordId)
        {
            //OutProcessRecord outProcessRecord = _dal.GetOutRecordById(inoutRecordId);
            //outProcessRecord.EndProductTime = DateTime.Now;
            //outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Complete;
            //outProcessRecord.OperateUser = inUser;
            //_dal.SaveData();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessRecord outProcessRecord = db.OutProcessRecord.FirstOrDefault(p => p.OutProcessRecordId == inoutRecordId);
                outProcessRecord.EndProductTime = DateTime.Now;
                outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Complete;
                outProcessRecord.OperateUser = inUser;
                db.SaveChanges();
            }
        }
        /// <summary>
        /// 恢复生产
        /// </summary>
        /// <param name="inUser"></param>
        /// <param name="inStrErr"></param>
        /// <param name="inoutRecordId"></param>
        public void RebootProduce(int inUser, int inoutRecordId)
        {
            OutProcessRecord outProcessRecord = _dal.GetOutRecordById(inoutRecordId);

            outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Doing;
            outProcessRecord.OperateUser = inUser;
            _dal.SaveData();
        }
        /// <summary>
        /// 出错暂停生产
        /// </summary>
        /// <param name="inUser"></param>
        /// <param name="inoutRecordId"></param>
        /// <param name="inStrErr"></param>
        public void PauseProduce(int inUser, string inOutBarCode, string inStrErr)
        {
            OutProcessRecord outProcessRecord = _dal.GetOutRecordByBarcode(inOutBarCode);

            outProcessRecord.Status2 = (int)OutProcessRecordStatus2.Error;
            outProcessRecord.OperateUser = inUser;
            outProcessRecord.Remark += inStrErr;
            _dal.SaveData();
        }


        public OutProcessRecord_Info GetReCordByCode(string productBarCode)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                OutProcessRecord outProcessRecord = db.OutProcessRecord.FirstOrDefault(o => o.orderInfo.OrderProductCode == productBarCode);
                if (outProcessRecord == null)
                {
                    return null;
                }
                OutProcessRecord_Info info = new OutProcessRecord_Info() { Status = outProcessRecord.Status, Status2 = outProcessRecord.Status2, OutProcessRecordId = outProcessRecord.OutProcessRecordId };
                return info;
            }
        }
        public OutProcessRecord_Info GetReCordByPhotoCode(string photoBarCode)
        {
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
                photoInfo photo = db.photoInfo.FirstOrDefault(p => p.PhotoBarCode == photoBarCode);
                if (photo == null)
                {
                    return null;
                }
                photo.orderInfoReference.Load();
                OutProcessRecord outProcessRecord = db.OutProcessRecord.FirstOrDefault(o => o.orderInfo.OrderProductId == photo.orderInfo.OrderProductId);
                if (outProcessRecord == null)
                {
                    return null;
                }
                OutProcessRecord_Info info = new OutProcessRecord_Info() { Status = outProcessRecord.Status, Status2 = outProcessRecord.Status2, OutProcessRecordId = outProcessRecord.OutProcessRecordId };
                return info;
            }
        }
        public OutProcessShop_Info GetShopById(int productId)
        {
            OutProcessShop outProcessRecord = _dal.GetShopByID(productId);
            if (outProcessRecord == null)
            {
                return null;
            }
            OutProcessShop_Info info = new OutProcessShop_Info() { OutShopId = outProcessRecord.OutShopId, OutShopName = outProcessRecord.OutShopName };
            return info;
        }
        public List<OutProcessShop_Info> GetAllInnerFactory()
        {
            List<OutProcessShop> lists = _dal.GetAllInnerFactory();

            List<OutProcessShop_Info> outProcessShopInfos = new List<OutProcessShop_Info>();
            foreach (OutProcessShop shop in lists)
            {
                OutProcessShop_Info info = new OutProcessShop_Info() { OutShopId = shop.OutShopId, OutShopName = shop.OutShopName };
                outProcessShopInfos.Add(info);
            }
            return outProcessShopInfos;
        }
        public DataTable GetPrintInfo(string inProductId)
        {
            return _dal.GetPrintInfo(inProductId);
        }

        /// <summary>
        /// 取得打印报表数据
        /// </summary>
        /// <param name="inGridId"></param>
        /// <param name="inProduct"></param>
        public DataSet GetReportdata(string inProduct, string inGridName)
        {
            DataSet ds = new DataSet();
            orderInfo product = null;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                if (inProduct.Substring(0, 1) == "2")
                {
                    product = Entities.orderInfo.FirstOrDefault(o => o.OrderProductCode == inProduct);
                }
                else
                {
                    photoInfo photo = Entities.photoInfo.FirstOrDefault(p=>p.PhotoBarCode ==inProduct);
                    if(photo ==null)
                        return ds;
                    if (!photo.orderInfoReference.IsLoaded)
                    {
                        photo.orderInfoReference.Load();
                    }
                    product = photo.orderInfo;
                }

                if (product == null)
                {
                    return ds;
                }
                if (!product.Tb_OrderBagReference.IsLoaded)
                {
                    product.Tb_OrderBagReference.Load();
                }
                if (!product.Tb_OrderBag.ShopReference.IsLoaded)
                {
                    product.Tb_OrderBag.ShopReference.Load();
                }


                DataTable infoTable = CreateInfoTable();
                infoTable.TableName = "infoTable";
                DataRow rowInfo = infoTable.NewRow();
                rowInfo["BagCode"] = product.Tb_OrderBag.BagCode;
                rowInfo["BagName"] = product.Tb_OrderBag.BagName;
                rowInfo["shopName"] = product.Tb_OrderBag.Shop.shopName;
                rowInfo["OrderProductCode"] = "*" + product.OrderProductCode + "*";
                rowInfo["OrderProductName"] = product.OrderProductName;
                rowInfo["CtmName"] = product.CtmName;
                rowInfo["productNum"] = product.productNum;
                rowInfo["remark"] = product.Remark;
                rowInfo["GridName"] = inGridName;
                rowInfo["OrderTime"] = product.Tb_OrderBag.OrderTime;

                infoTable.Rows.Add(rowInfo);
                if (product.Priority >= 11)
                {
                    rowInfo["Priority"] = "特急件";
                }
                else if (product.Priority < 11 && product.Priority >= 6)
                {
                    rowInfo["Priority"] = "急件";
                }
                else
                {
                    rowInfo["Priority"] = "";
                }

                DataTable photoTable = CreatePhotoInfoTable();
                Dictionary<string, int> photos = new Dictionary<string, int>();
                //Dictionary<string, int> photosCM = new Dictionary<string, int>();
                if (!product.photoInfo.IsLoaded)
                {
                    product.photoInfo.Load();
                }
                foreach (photoInfo list in product.photoInfo)
                {
                    string photoSize = "" + Math.Round(list.Width, 2) + "×" + Math.Round(list.Height, 2) + "in(" + Math.Round(list.Width * 2.54, 2) + "×" + Math.Round(list.Height * 2.54, 2) + "cm)";
                    string photoSize1 = "" + Math.Round(list.Height, 2) + "×" + Math.Round(list.Width, 2) + "in(" + Math.Round(list.Height * 2.54, 2) + "×" + Math.Round(list.Width * 2.54, 2) + "cm)";

                    //string photoSizeCM = Math.Round(list.Width * 2.54, 2) + "×" + Math.Round(list.Height, 2);
                    if (photos.Keys.Contains(photoSize))
                    {
                        photos[photoSize] += 1;
                    }
                    else if (photos.Keys.Contains(photoSize1))
                    {
                        photos[photoSize1] += 1;
                    }
                    else
                    {
                        photos.Add(photoSize, 1);
                        //photosCM.Add(photoSizeCM, 1);
                    }
                }
                foreach (var list in photos)
                {
                    DataRow row = photoTable.NewRow();
                    row["Size"] = list.Key;
                    row["Num"] = list.Value;
                    photoTable.Rows.Add(row);

                }
                photoTable.TableName = "photoTable";

                if (!product.WorkOrderFlowList.IsLoaded)
                {
                    product.WorkOrderFlowList.Load();
                }
                //List<WorkOrderFlowList> flows = _dal.GetAllFlowByProductId(inProduct).OrderBy(p => p.WorkOrder).ToList();
                string strFlow = string.Empty;
                foreach (WorkOrderFlowList list in product.WorkOrderFlowList.OrderBy(p => p.WorkOrder))
                {
                    if (!list.tb_TechReference.IsLoaded)
                    {
                        list.tb_TechReference.Load();
                    }
                    strFlow = strFlow + list.tb_Tech.TechName + "->";
                }
                strFlow = strFlow.Trim("->".ToCharArray());
                DataTable flowTable = CreateTechFlow();
                //foreach (WorkOrderFlowList list in flows)
                //{
                //    if (!list.tb_TechReference.IsLoaded)
                //    {
                //        list.tb_TechReference.Load();
                //    }
                //    users user = _dal.GetUserById(list.UserId);
                //    DataRow row = flowTable.NewRow();
                //    row["TechName"] = list.tb_Tech.TechName;
                //    if (user != null)
                //    {
                //        row["UserName"] = user.UserName;
                //    }
                //    else
                //    {
                //        row["UserName"] = "";
                //    }
                //    flowTable.Rows.Add(row);
                //}
                DataRow rowt = flowTable.NewRow();
                rowt["TechName"] = strFlow;
                rowt["UserName"] = "0";
                flowTable.Rows.Add(rowt);
                flowTable.TableName = "flowTable";

                ds.Tables.Add(photoTable);
                ds.Tables.Add(infoTable);
                ds.Tables.Add(flowTable);
            }
            return ds;
        }

        /// <summary>
        ///  基本信息表
        /// </summary>
        /// <returns></returns>
        public DataTable CreateInfoTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("BagCode", typeof(string));
            table.Columns.Add("BagName", typeof(string));
            table.Columns.Add("shopName", typeof(string));
            table.Columns.Add("OrderProductCode", typeof(string));
            table.Columns.Add("OrderProductName", typeof(string));
            table.Columns.Add("CtmName", typeof(string));
            table.Columns.Add("productNum", typeof(int));
            table.Columns.Add("remark", typeof(string));
            table.Columns.Add("GridName", typeof(string));
            table.Columns.Add("Priority", typeof(string));
            table.Columns.Add("OrderTime", typeof(string));
            return table;

        }

        /// <summary>
        /// 照片信息
        /// </summary>
        /// <returns></returns>
        public DataTable CreatePhotoInfoTable()
        {
            DataTable table = new DataTable();
            table.Columns.Add("Size", typeof(string));
            //table.Columns.Add("Size1", typeof(string));
            table.Columns.Add("Num", typeof(int));
            return table;
        }

        /// <summary>
        /// 流程信息
        /// </summary>
        /// <returns></returns>
        public DataTable CreateTechFlow()
        {
            DataTable table = new DataTable();
            table.Columns.Add("TechName", typeof(string));
            table.Columns.Add("UserName", typeof(string));
            return table;
        }

        /// <summary>
        /// 检查产品是否存在
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public bool IsProductExist(string productCode)
        {
            bool flag = true;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                orderInfo oi = Entities.orderInfo.FirstOrDefault(o => o.OrderProductCode == productCode);
                if (oi == null)
                {
                    flag = false;
                }
            }

            return flag;
        }

        /// <summary>
        /// 检查产品是否存在
        /// </summary>
        /// <param name="productCode"></param>
        /// <returns></returns>
        public bool IsPhotoExist(string photoBarCode)
        {
            bool flag = true;
            using (newSampleYPDBEntities Entities = new newSampleYPDBEntities())
            {
                photoInfo photo = Entities.photoInfo.FirstOrDefault(o => o.PhotoBarCode == photoBarCode);
                if (photo == null)
                {
                    flag = false;
                }
            }

            return flag;
        }

        public ProduceView GetProduceView(int OutProcessRecordId)
        {
            ProduceView newView = new ProduceView();
            using (newSampleYPDBEntities db = new newSampleYPDBEntities())
            {
            
                OutProcessRecord outProcessRecords = db.OutProcessRecord.FirstOrDefault(p => p.OutProcessRecordId == OutProcessRecordId);
                
                    if (!outProcessRecords.orderInfoReference.IsLoaded)
                    {
                        outProcessRecords.orderInfoReference.Load();
                    }
                    orderInfo product = outProcessRecords.orderInfo;
                    product.Tb_ProductReference.Load();
                    Tb_Product productType = product.Tb_Product;
                    newView.Product = new OrderInfo_Info() { ProductId = product.OrderProductId, CompleteTime = product.CompleteTime, CtmName = product.CtmName, Height = product.Height, Width = product.Width, OrderProductCode = product.OrderProductCode, Remark = product.Remark, OrderProductName = product.OrderProductName, PageNum = product.PageNum, OrderPath = product.OrderPath };
                    newView.ProductType = new Tb_Product_Info() { ProuctId = productType.ProuctId, ProductName = productType.ProductName };

                    if (!product.Tb_OrderBagReference.IsLoaded)
                    {
                        product.Tb_OrderBagReference.Load();
                    }
                    Tb_OrderBag orderBag = product.Tb_OrderBag;
                    orderBag.ShopReference.Load();
                    newView.Order = new Tb_OrderBag_Info() { BagCode = orderBag.BagCode, BagName = orderBag.BagName, OrderBagId = orderBag.OrderBagId, ShopID = orderBag.Shop.ShopId, ShopName = orderBag.Shop.shopName, FetchTime = orderBag.FetchTime };
                    newView.ProduceInfo = new OutProcessRecord_Info() { CreateTime = outProcessRecords.CreateTime, EndProductTime = outProcessRecords.EndProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)outProcessRecords.EndProductTime, StartProductTime = outProcessRecords.StartProductTime == null ? Convert.ToDateTime("2000-01-01") : (DateTime)outProcessRecords.StartProductTime, Status = outProcessRecords.Status, Status2 = outProcessRecords.Status2, OutRemark = outProcessRecords.OutRemark, Remark = outProcessRecords.Remark };

                    outProcessRecords.OutProcessShopReference.Load();
                    OutProcessShop shop = outProcessRecords.OutProcessShop;
                    newView.Factory = new OutProcessShop_Info() { OutShopId = shop.OutShopId, OutShopName = shop.OutShopName };
                    //switch (newView.ProduceInfo.Status)
                    //{
                    //    case (int)OutProcessRecordStatus.Outing:
                    //        {
                    //            switch (newView.ProduceInfo.Status2)
                    //            {
                    //                case (int)OutProcessRecordStatus2.Doing:
                    //                    newView.Status = "正在生产";
                    //                    break;
                    //                case (int)OutProcessRecordStatus2.Error:
                    //                    newView.Status = "暂停生产";
                    //                    break;
                    //                case (int)OutProcessRecordStatus2.Complete:
                    //                    newView.Status = "生产完成";
                    //                    break;
                    //                default:
                    //                    newView.Status = "等待接收";
                    //                    break;
                    //            }
                    //        }
                    //        break;
                    //    case (int)OutProcessRecordStatus.InGrid:
                    //        {
                    //            newView.Status = "未打印(在格子中)";
                    //        }
                    //        break;
                    //    case (int)OutProcessRecordStatus.OutWaitFenDui:
                    //        {
                    //            newView.Status = "未打印(等待分堆)";
                    //        }

                    //        break;
                    //}
                    productType.Tb_OutShop_ProductCategory.Load();
                    Tb_OutShop_ProductCategory pc = productType.Tb_OutShop_ProductCategory.Where(p => p.OutShopId == shop.OutShopId).FirstOrDefault();
                    if (pc != null)
                    {
                        newView.TimeSpan = pc.NeedTime;
                    }
                    else
                    {
                        newView.TimeSpan = 0;
                    }

                    return newView;
            }
          
        }
    }
}
